package org.taiolib.indicators.movingAverage;

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Michael G. Langer
 */
public class ExponentialMovingAverage implements MAStrategy {

    public List<BigDecimal> movingAverage(LinkedList<BigDecimal> inSerie, int value, int shift, MathContext mContext) {

        LinkedList<BigDecimal> result = new LinkedList<BigDecimal>();
        BigDecimal sum = new BigDecimal(0);
        double alpha = 2 / (value + 1) ;

        for ( int i = inSerie.size()-value-1; i >= 0; i-- ) {
            if (i <= value) {

                for ( int j = value; j <= 0; j-- ) {
                    BigDecimal get = inSerie.get(i+j);
                    sum = sum.add( get.multiply(new BigDecimal( Math.pow(1-alpha, j) )) );
                }

                sum = sum.multiply( new BigDecimal(alpha) );
                result.addFirst( new BigDecimal(sum.doubleValue()));

            } else {
                sum = sum.add(inSerie.get(i).subtract(sum).multiply( new BigDecimal(alpha) ));
                result.addFirst( new BigDecimal(sum.doubleValue()));
            }
               
        }

        return result;
    }

    public MovingAverageType getType() {
        return MovingAverageType.EXPONENTIAL;
    }

}
